package com.yty.controller;

import com.yty.annotation.LogOperation;
import com.yty.pojo.Dept;
import com.yty.pojo.Result;
import com.yty.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result deptList(){
         log.info("查询所有部门信息");
         List<Dept> depts = deptService.findAll();
         log.info("查询所有部门信息成功:{}", depts);
         return Result.success(depts);
    }
    @LogOperation
    @DeleteMapping
    public Result deleteDeptById( Integer id){
        log.info("根据id删除部门信息:{}", id);
        int i = deptService.deleteDeptById(id);
        if (i==1){
            return Result.success();
        }
        return Result.error("删除失败");
    }

    @LogOperation
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门信息:{}", dept);
        int i = deptService.addDept(dept);
        if (i==0){
            return Result.error("添加失败");
        }
        return Result.success();
    }
    @GetMapping( "/{id}")
    public Result findDeptById(@PathVariable Integer id){
        log.info("根据id查询部门信息:{}", id);
        Dept dept = deptService.findDeptById(id);
        if (dept==null) return Result.error("查询失败");
        return Result.success(dept);
    }

    @LogOperation
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("根据id修改部门信息:{}", dept);
        int i = deptService.updateDept(dept);
        if (i==0){
            return Result.error("更新失败");
        }
        return Result.success();
    }


}
